/********************************************************************
***	Author : Franck Tomeba - Domenico DE FANO
***
********************************************************************/
#include "TestApp.h"
#include "sleep.h"
#include "stdio.h"

int main(void)
{
	unsigned int value;
	int counter = 20;

	print("\n\rCoprocessor Simulation 1\n\r");
	print("Do 1 monitoring round and result 4 result buffer registers\n\r");
	
	//enable_interrupt();
	
	print("Set Monitoring Length : 0x00000040\n\r");
	write_reg(OFFSET_MON_LEN, 0x00000040);

	print("Set Scan Period : 0x0000003B\n\r");
	write_reg(OFFSET_SCAN_PERIOD, 0x0000003B);
	
	print("Set Gateway Configuration : 0x07\n\r");
	write_reg(OFFSET_GW_CONFIG, 0x00000007);
	
	print("Set Configuration Buffer 1 : 0x0000000\n\r");
	write_reg(OFFSET_CONFIG_BUFFER_1, 0x0000000);
	
	print("Set Configuration Buffer 2 : 0x0000000\n\r");	
	write_reg(OFFSET_CONFIG_BUFFER_2, 0x0000000);
	
	print("Set Configuration Buffer 3 : 0x0000000\n\r");	
	write_reg(OFFSET_CONFIG_BUFFER_3, 0x0000000);
	
	//write_reg(OFFSET_IRQ, 0);
	print("Start Monitoring\n\r");
	write_reg(OFFSET_CMD, CMD_START);
	write_reg(OFFSET_CMD, CMD_NOP);
	
	print("Busy Waiting on Interrupt\n\r");
	value = 0;
	
	do {
		
		do {
			value = read_reg(OFFSET_IRQ);
			print("\n\rIRQ Reg :");
			putnum(value);
		} while ( value == 0 );
				
		print("\n\rRead Monitoring Values\n\r");
		
		value = read_reg(OFFSET_STATUS);
		print("\n\rSTATUS : ");
		putnum(value & 0xF);
		
		value = read_reg(OFFSET_RESULT_BUFFER_1);
		print("\n\rResult Buffer 1 : ");
		putnum(value); 
		
		value = read_reg(OFFSET_RESULT_BUFFER_2);
		print("\n\rResult Buffer 2 : ");
		putnum(value); 
		
		value = read_reg(OFFSET_RESULT_BUFFER_3);
		print("\n\rResult Buffer 3 : ");
		putnum(value); 
	
		value = read_reg(OFFSET_RESULT_BUFFER_4);
		print("\n\rResult Buffer 4 : ");
		putnum(value); 
		
		if ( counter == 10 )
		{
			print("Set Monitoring Length : 0x00000027\n\r");
			write_reg(OFFSET_MON_LEN, 0x00000027);
			
			print("Set Configuration Buffer 1 : 0xFEDCBA9\n\r");
			write_reg(OFFSET_CONFIG_BUFFER_1, 0xFEDCBA9);
			
			print("Set Configuration Buffer 2 : 0x8765432\n\r");	
			write_reg(OFFSET_CONFIG_BUFFER_2, 0x8765432);
			
			print("Set Configuration Buffer 3 : 0x1000000\n\r");	
			write_reg(OFFSET_CONFIG_BUFFER_3, 0x1000000);		
		}
		sleep(10);

		write_reg(OFFSET_IRQ, 0);
		counter--;
	} while (counter != 0);
	
	print("\n\rStop Monitoring\n\r");
	write_reg(OFFSET_CMD, CMD_STOP);
	write_reg(OFFSET_CMD, CMD_NOP);
	
	return 1;
}
